package com.coisini.service.impl;

import com.coisini.context.BaseContext;
import com.coisini.dto.ShoppingCartDTO;
import com.coisini.entity.Setmeal;
import com.coisini.entity.ShoppingCart;
import com.coisini.mapper.DishMapper;
import com.coisini.mapper.SetmealMapper;
import com.coisini.mapper.ShoppingCartMapper;
import com.coisini.service.ShoppingCartService;
import com.coisini.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService{

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     * 1.动态查询表里面是否有数据
     * 2.如果有数据并且该数据跟传递的数据相同，则进行修改操作，将数量改为原来数量+1
     * 3.如果没有数据，则进行新增数据
     * 4.新增数据时，判断传递的数据是说明类型，（菜品还是套餐），做出相应的操作
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //1.动态查询数据库
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        List<ShoppingCart> list= shoppingCartMapper.list(shoppingCart);
        
        //2.对查询的数据进行判断
        if (list != null && list.size()>0){
            //说明有相同的数据，在原有的基础上修改表的数量字段
            shoppingCart=list.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartMapper.updateNumberById(shoppingCart);
        }else {
            //没有数据 需要判断加入购物车的是菜品还是套餐
            Long dishId = shoppingCart.getDishId();
            Long setmealId = shoppingCart.getSetmealId();
            if (dishId != null){
                //加入的是菜品
                //需要根据菜品id查询菜品的价格，名称，图片信息
                DishVO dishVO = dishMapper.getById(dishId);
                shoppingCart.setName(dishVO.getName());
                shoppingCart.setImage(dishVO.getImage());
                shoppingCart.setAmount(dishVO.getPrice());
            }else {
                //加入的是套餐
                //根据套餐的id查询套餐的信息
                Setmeal setmeal = setmealMapper.getById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }

            //补充表中其他的字段
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCart.setCreateTime(LocalDateTime.now());
            //带哦用mapper接口新增数据
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 根据用户id查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> showShoppoingCart() {
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId()).build();
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }


    /**
     * 清空购物车
     * 根据用户id清空购物车
     */
    @Override
    public void cleanShoppingCart() {
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .build();
        shoppingCartMapper.delete(shoppingCart);
    }

    /**
     * 删除购物车中的商品
     * 1.当商品数量为多个时，需要更新商品的数量
     * 2.当商品的数量只有一份时，删除商品需要将表中数据删除
     * @param shoppingCartDTO
     */
    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //1.动态查询数据库
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);

        //注： 如果表中有其他用户的数据跟当前的用户购物车信息一样，此时就有问题
        //将属性值userId设置为当前用户的
        shoppingCart.setUserId(BaseContext.getCurrentId());

        List<ShoppingCart> list= shoppingCartMapper.list(shoppingCart);
        if (list != null && list.size() > 0){
           shoppingCart = list.get(0);

           //对shoppingCart的个数属性进行判断
            if (shoppingCart.getNumber() == 1){
                //当份数只有一个时，直接删除数据
                shoppingCartMapper.delete(shoppingCart);
            } else if (shoppingCart.getNumber() > 1) {
                //当商品数量大于1时，需要更新数据表中的数量字段
                shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                shoppingCartMapper.updateNumberById(shoppingCart);
            }
        }
    }
}
